using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCNoun : RepositoryItemUI
	{
		#region Initialization

		public UCNoun(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();

			init();
		}
		private void init()
		{
			DataTable dt = Global.Data.GetDataTypes();
			cboDataType.BeginUpdate();
			cboDataType.Items.Clear();
			foreach ( DataRow row in dt.Rows ) {
				cboDataType.Items.Add(row["datatype"].ToString());
			}
			cboDataType.EndUpdate();

		}

		#endregion

		#region Public Members

		#region Properties
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Noun _noun = null;
		#endregion

		#region Methods
		private void load()
		{
			if ( _noun == null )
				return;

			txtGVarName.Text = _noun.Name;
			txtGVarDesc.Text = _noun.Description;
			cboDataType.Text = _noun.DataType;

			cboDataType.Visible = ( _noun.Type == Noun.NounType.Noun );
			lblDataType.Visible = ( _noun.Type == Noun.NounType.Noun );

			if ( string.IsNullOrEmpty(cboDataType.Text) )
				cboDataType.SelectedIndex = cboDataType.FindString("string");

			tvUsedByHirarchy.RepositoryItem = _noun;
		}
		private void save()
		{
			if ( _noun == null )
				_noun = new Noun();

			_noun.Reload();

			_noun.Name = txtGVarName.Text;
			_noun.Description = txtGVarDesc.Text;
			_noun.DataType = cboDataType.Text;
		}
		#endregion

		#region Event Handlers
		#endregion

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtGVarName.Text = value.ToString();
			}
		}

		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _noun;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is Noun ) {
					_noun = (Noun)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sNoun; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}

		private void txtGVarName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}
	}
}
